

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.ops.DynamicRNN &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="mindspore.ops.Elu" href="mindspore.ops.Elu.html" />
    <link rel="prev" title="mindspore.ops.DynamicGRUV2" href="mindspore.ops.DynamicGRUV2.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../mindspore.ops.html">mindspore.ops</a> &raquo;</li>
        
      <li>mindspore.ops.DynamicRNN</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
          
            <a href="../../_sources/api_python/ops/mindspore.ops.DynamicRNN.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="mindspore-ops-dynamicrnn">
<h1>mindspore.ops.DynamicRNN<a class="headerlink" href="#mindspore-ops-dynamicrnn" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="mindspore.ops.DynamicRNN">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.ops.</code><code class="sig-name descname">DynamicRNN</code><span class="sig-paren">(</span><em class="sig-param">cell_type=&quot;LSTM&quot;</em>, <em class="sig-param">direction=&quot;UNIDIRECTIONAL&quot;</em>, <em class="sig-param">cell_depth=1</em>, <em class="sig-param">use_peephole=False</em>, <em class="sig-param">keep_prob=1.0</em>, <em class="sig-param">cell_clip=-1.0</em>, <em class="sig-param">num_proj=0</em>, <em class="sig-param">time_major=True</em>, <em class="sig-param">activation=&quot;tanh&quot;</em>, <em class="sig-param">forget_bias=0.0</em>, <em class="sig-param">is_training=True</em><span class="sig-paren">)</span><a class="reference internal" href="../../_modules/mindspore/ops/operations/nn_ops.html#DynamicRNN"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#mindspore.ops.DynamicRNN" title="Permalink to this definition">¶</a></dt>
<dd><p>Applies a recurrent neural network to the input.
Only long short-term memory (LSTM) is supported currently.</p>
<div class="math notranslate nohighlight">
\[\begin{split}\begin{array}{ll} \\
    i_{t+1} = \sigma(W_{ix} x_{t+1} + b_{ix} + W_{ih} h_{(t)} + b_{ih}) \\
    f_{t+1} = \sigma(W_{fx} x_{t+1} + b_{fx} + W_{fh} h_{(t)} + b_{fh}) \\
    \tilde{c}_{t+1} = \tanh(W_{cx} x_{t+1} + b_{cx} + W_{ch} h_{(t)} + b_{ch}) \\
    o_{t+1} = \sigma(W_{ox} x_{t+1} + b_{ox} + W_{oh} h_{(t)} + b_{oh}) \\
    c_{t+1} = f_{t+1} * c_{(t)} + i_t * \tilde{c}_{t+1} \\
    h_{t+1} = o_{t+1} * \tanh(c_{t+1}) \\
\end{array}\end{split}\]</div>
<p>where <span class="math notranslate nohighlight">\(h_{t+1}\)</span> is the hidden state at time <cite>t+1</cite>, <span class="math notranslate nohighlight">\(x_{t+1}\)</span> is the input
at time <cite>t+1</cite>, <span class="math notranslate nohighlight">\(h_{t}\)</span> is the hidden state of the layer
at time <cite>t</cite> or the initial hidden state at time <cite>0</cite>,
<span class="math notranslate nohighlight">\(\sigma\)</span> is the sigmoid function, and <span class="math notranslate nohighlight">\(*\)</span> is the Hadamard product. <span class="math notranslate nohighlight">\(W, b\)</span>
are learnable weights between the output and the input in the formula. For instance,
<span class="math notranslate nohighlight">\(W_{ix}, b_{ix}\)</span> are the weight and bias used to transform from input <span class="math notranslate nohighlight">\(x\)</span> to <span class="math notranslate nohighlight">\(i\)</span>.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>cell_type</strong> (<a class="reference external" href="https://docs.python.org/library/stdtypes.html#str" title="(in Python v3.8)"><em>str</em></a>) – A string identifying the cell type in the operator. Default: ‘LSTM’.
Only ‘LSTM’ is currently supported.</p></li>
<li><p><strong>direction</strong> (<a class="reference external" href="https://docs.python.org/library/stdtypes.html#str" title="(in Python v3.8)"><em>str</em></a>) – A string identifying the direction in the operator. Default: ‘UNIDIRECTIONAL’.
Only ‘UNIDIRECTIONAL’ is currently supported.</p></li>
<li><p><strong>cell_depth</strong> (<a class="reference external" href="https://docs.python.org/library/functions.html#int" title="(in Python v3.8)"><em>int</em></a>) – An integer identifying the cell depth in the operator. Default: 1.</p></li>
<li><p><strong>use_peephole</strong> (<a class="reference external" href="https://docs.python.org/library/functions.html#bool" title="(in Python v3.8)"><em>bool</em></a>) – A bool identifying if use peephole in the operator. Default: False.</p></li>
<li><p><strong>keep_prob</strong> (<a class="reference external" href="https://docs.python.org/library/functions.html#float" title="(in Python v3.8)"><em>float</em></a>) – A float identifying the keep prob in the operator. Default: 1.0.</p></li>
<li><p><strong>cell_clip</strong> (<a class="reference external" href="https://docs.python.org/library/functions.html#float" title="(in Python v3.8)"><em>float</em></a>) – A float identifying the cell clip in the operator. Default: -1.0.</p></li>
<li><p><strong>num_proj</strong> (<a class="reference external" href="https://docs.python.org/library/functions.html#int" title="(in Python v3.8)"><em>int</em></a>) – An integer identifying the number projection in the operator. Default: 0.</p></li>
<li><p><strong>time_major</strong> (<a class="reference external" href="https://docs.python.org/library/functions.html#bool" title="(in Python v3.8)"><em>bool</em></a>) – A bool identifying the time major in the operator. Default: True.
Only <cite>True</cite> is currently supported.</p></li>
<li><p><strong>activation</strong> (<a class="reference external" href="https://docs.python.org/library/stdtypes.html#str" title="(in Python v3.8)"><em>str</em></a>) – A string identifying the type of activation function in the operator. Default: ‘tanh’.
Only ‘tanh’ is currently supported.</p></li>
<li><p><strong>forget_bias</strong> (<a class="reference external" href="https://docs.python.org/library/functions.html#float" title="(in Python v3.8)"><em>float</em></a>) – A float identifying the forget bias in the operator. Default: 0.0.</p></li>
<li><p><strong>is_training</strong> (<a class="reference external" href="https://docs.python.org/library/functions.html#bool" title="(in Python v3.8)"><em>bool</em></a>) – A bool identifying is training in the operator. Default: True.</p></li>
</ul>
</dd>
</dl>
<dl class="simple">
<dt>Inputs:</dt><dd><ul class="simple">
<li><p><strong>x</strong> (Tensor) - Current words. Tensor of shape <span class="math notranslate nohighlight">\((num\_step, batch\_size, input\_size)\)</span>.
The data type must be float16.</p></li>
<li><p><strong>w</strong> (Tensor) - Weight. Tensor of shape <span class="math notranslate nohighlight">\((input\_size + hidden\_size, 4 * hidden\_size)\)</span>.
The data type must be float16.</p></li>
<li><p><strong>b</strong> (Tensor) - Bias. Tensor of shape <span class="math notranslate nohighlight">\((4 * hidden\_size)\)</span>.
The data type must be float16 or float32.</p></li>
<li><p><strong>seq_length</strong> (Tensor) - The length of each batch. Tensor of shape <span class="math notranslate nohighlight">\((batch\_size, )\)</span>.
Only <cite>None</cite> is currently supported.</p></li>
<li><p><strong>init_h</strong> (Tensor) - Hidden state of initial time. Tensor of shape <span class="math notranslate nohighlight">\((1, batch\_size, hidden\_size)\)</span>.
The data type must be float16.</p></li>
<li><p><strong>init_c</strong> (Tensor) - Cell state of initial time. Tensor of shape <span class="math notranslate nohighlight">\((1, batch\_size, hidden\_size)\)</span>.
The data type must be float16.</p></li>
</ul>
</dd>
<dt>Outputs:</dt><dd><ul class="simple">
<li><p><strong>y</strong> (Tensor) - A Tensor of shape <span class="math notranslate nohighlight">\((num\_step, batch\_size, hidden\_size)\)</span>.
Has the same type with input <cite>b</cite>.</p></li>
<li><p><strong>output_h</strong> (Tensor) - A Tensor of shape <span class="math notranslate nohighlight">\((num\_step, batch\_size, hidden\_size)\)</span>.
With data type of float16.</p></li>
<li><p><strong>output_c</strong> (Tensor) - A Tensor of shape <span class="math notranslate nohighlight">\((num\_step, batch\_size, hidden\_size)\)</span>.
Has the same type with input <cite>b</cite>.</p></li>
<li><p><strong>i</strong> (Tensor) - A Tensor of shape <span class="math notranslate nohighlight">\((num\_step, batch\_size, hidden\_size)\)</span>.
Has the same type with input <cite>b</cite>.</p></li>
<li><p><strong>j</strong> (Tensor) - A Tensor of shape <span class="math notranslate nohighlight">\((num\_step, batch\_size, hidden\_size)\)</span>.
Has the same type with input <cite>b</cite>.</p></li>
<li><p><strong>f</strong> (Tensor) - A Tensor of shape <span class="math notranslate nohighlight">\((num\_step, batch\_size, hidden\_size)\)</span>.
Has the same type with input <cite>b</cite>.</p></li>
<li><p><strong>o</strong> (Tensor) - A Tensor of shape <span class="math notranslate nohighlight">\((num\_step, batch\_size, hidden\_size)\)</span>.
Has the same type with input <cite>b</cite>.</p></li>
<li><p><strong>tanhct</strong> (Tensor) - A Tensor of shape <span class="math notranslate nohighlight">\((num\_step, batch\_size, hidden\_size)\)</span>.
Has the same type with input <cite>b</cite>.</p></li>
</ul>
</dd>
</dl>
<dl class="field-list simple">
<dt class="field-odd">Raises</dt>
<dd class="field-odd"><ul class="simple">
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#TypeError" title="(in Python v3.8)"><strong>TypeError</strong></a> – If <cite>cell_type</cite>, <cite>direction</cite> or <cite>activation</cite> is not a str.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#TypeError" title="(in Python v3.8)"><strong>TypeError</strong></a> – If <cite>cell_depth</cite> or <cite>num_proj</cite> is not an int.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#TypeError" title="(in Python v3.8)"><strong>TypeError</strong></a> – If <cite>keep_prob</cite>, <cite>cell_clip</cite> or <cite>forget_bias</cite> is not a float.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#TypeError" title="(in Python v3.8)"><strong>TypeError</strong></a> – If <cite>use_peehpole</cite>, <cite>time_major</cite> or <cite>is_training</cite> is not a bool.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#TypeError" title="(in Python v3.8)"><strong>TypeError</strong></a> – If <cite>x</cite>, <cite>w</cite>, <cite>b</cite>, <cite>seq_length</cite>, <cite>init_h</cite> or <cite>init_c</cite> is not a Tensor.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#TypeError" title="(in Python v3.8)"><strong>TypeError</strong></a> – If dtype of <cite>x</cite>, <cite>w</cite>, <cite>init_h</cite> or <cite>init_c</cite> is not float16.</p></li>
<li><p><a class="reference external" href="https://docs.python.org/library/exceptions.html#TypeError" title="(in Python v3.8)"><strong>TypeError</strong></a> – If dtype of <cite>b</cite> is neither float16 nor float32.</p></li>
</ul>
</dd>
</dl>
<dl class="simple">
<dt>Supported Platforms:</dt><dd><p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code></p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">64</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float16</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">w</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">96</span><span class="p">,</span> <span class="mi">128</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float16</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">128</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float16</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">init_h</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">32</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float16</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">init_c</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">32</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float16</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dynamic_rnn</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">DynamicRNN</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">output</span> <span class="o">=</span> <span class="n">dynamic_rnn</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">w</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="n">init_h</span><span class="p">,</span> <span class="n">init_c</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">output</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="go">(2, 16, 32)</span>
</pre></div>
</div>
</dd></dl>

</div>


           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="mindspore.ops.Elu.html" class="btn btn-neutral float-right" title="mindspore.ops.Elu" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
        <a href="mindspore.ops.DynamicGRUV2.html" class="btn btn-neutral float-left" title="mindspore.ops.DynamicGRUV2" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>